﻿
/// <reference path="vswd-ext_2.2.js" />

/********************poi.js*********************************

 *Create Date: 2010-3
 *Description : this js file use ext js to create AJAX POI function window 
*********************************************************************/
function submitAddPOI(txtname, lat, lng, iconindex) {
    //txtname has no use because firefox can use document.all ,can't send back the name

    var obj = document.getElementById("textpoi");
    var name = obj.value;
    Ext.Ajax.request({
        url: 'HandlerAddPOI',
        method: 'post',
        params: {
            poiname: name,
            latitude: lat,
            longitude: lng,
            iconindex: iconindex
        },
        success: function(btn) {
            var val = btn.responseText;
            if (val == 0) {
                Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
                Ext.Msg.show({
                    title: _TrackerMain_Title,
                    msg: _POI_AddSuccess,
                    buttons: Ext.Msg.OK,
                    animEl: 'elId',
                    icon: Ext.MessageBox.INFO
                });
                if (imap != null)
                    imap.cancelAddEditPOI();
                showPOI("");
            }
            else if (val == -100) {
                Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
                Ext.Msg.show({
                    title: _TrackerMain_Title,
                    msg: _POI_POINumberLimit,
                    buttons: Ext.Msg.OK,
                    animEl: 'elId',
                    icon: Ext.MessageBox.INFO
                });
            }
            else {
                Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
                Ext.Msg.show({
                    title: _TrackerMain_Title,
                    msg: _POI_AddFailed,
                    buttons: Ext.Msg.OK,
                    animEl: 'elId',
                    icon: Ext.MessageBox.INFO
                });
            }


        }
    });
}

function submitEditPOI(poiid, txtname, lat, lng, iconindex) {
    var obj = document.getElementById("textpoi");
    var name = obj ? obj.value : txtname;
    Ext.Ajax.request({
        url: 'HandlerEditMovePOI',
        method: 'post',
        params: {
            poiid: poiid,
            poiname: name,
            latitude: lat,
            longitude: lng,
            iconindex: iconindex
        },
        success: function(btn) {
            var val = btn.responseText;
            if (val == 0) {
                Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
                Ext.Msg.show({
                    title: _TrackerMain_Title,
                    msg: _POI_EditSuccess,
                    buttons: Ext.Msg.OK,
                    animEl: 'elId',
                    icon: Ext.MessageBox.INFO
                });
                if (imap != null)
                    imap.cancelAddEditPOI();
                showPOI("");
            }

            else {
                Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
                Ext.Msg.show({
                    title: _TrackerMain_Title,
                    msg: _POI_EditFailed,
                    buttons: Ext.Msg.OK,
                    animEl: 'elId',
                    icon: Ext.MessageBox.INFO
                });
            }


        }
    });
}


function cancelAddEditPOI() {
    if (imap != null)
        imap.cancelAddEditPOI();
}

function showPOI(key,rmap) {
    var poifields = ["poiid", "poiname", "latitude", "longitude", "iconindex"];
    var globalpoistore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: "HandlerSearchPOI",
            method: "POST"
        }),
        reader: new Ext.data.JsonReader({
            fields: poifields,
            root: "data",
            totalProperty: "totalCount"
        })
    });
    globalpoistore.addListener('load', function(obj, records) {
        if (rmap == null) {

            if (imap != null) {
                imap.drawPOIbyRecords(records);
            }
        }
        else {
            rmap.drawPOIbyRecords(records);
        }
    });  
 
    globalpoistore.reload({
        params: {
            poikey: key
            }
    });

}

function POIWindow() {

    var editor = new Ext.ux.grid.RowEditor({
        saveText: _CustomerConfigResource_Update,
        cancelText: _CustomerConfigResource_Cancel
    });


    var poifields = ["poiid", "poiname", "latitude", "longitude", "iconindex"];
    var poistore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: "HandlerSearchPOI",
            method: "POST"
        }),
        reader: new Ext.data.JsonReader({
            fields: poifields,
            root: "data",
            totalProperty: "totalCount"
        })
    });
    
    var txtKey = new Ext.form.TextField({
        allowBlank: true,
        maxLength: 32,
        emptyText: _POI_SearchPOI,
        maxText: _Message_MaxText+' 32'
    });

    var chkShowAllPOI = new Ext.form.Checkbox({
        boxLabel: _POI_ShowAllPOI
    });

    var autoshowpoi = getCookie("showallpoi") == 'false' ? false : true;
    chkShowAllPOI.setValue((autoshowpoi == null) ? false : autoshowpoi);

    chkShowAllPOI.addListener('check', function(obj, v) {
        setCookie('showallpoi', v, 365);
        if (imap != null)
            imap.hidePOI();
        if (v) {
            showPOI("");
        }
    });

    var tbar = new Ext.Toolbar({
        items: [
        //_POI_Key,
            txtKey,
            {
                tooltip: { text: _POI_Search, autoHide: true },
                icon: 'images/search16.gif',
                handler: function() {
                    poistore.reload({
                        params: {
                            poikey: txtKey.getValue()
                        }
                    });
                }
            },
            {
                xtype: 'tbseparator', width: 5
            },
            {
                tooltip: { text: _POI_AddNew, autoHide: true },
                icon: 'images/add.png',
                handler: function() {
                    if (imap != null) {
                        imap.beginAddPOI(_POI_Submit, _POI_Cancel);
                    }
                }
            },
            {
                tooltip: { text: _POI_Move, autoHide: true },
                icon: 'images/move.gif',
                handler: function() {
                    var records = grdPOI.getSelectionModel().getSelections();
                    if (records.length > 0) {
                        var record = records[0];
                        if (imap != null)
                            imap.beginEditMovePOI(record.get("poiid"), record.get("latitude"), record.get("longitude"), record.get("iconindex"), record.get("poiname"), _POI_Submit, _POI_Cancel);
                    }
                    else {
                        Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
                        Ext.Msg.show({
                            title: _TrackerMain_Title,
                            msg: _POI_SelectOne,
                            buttons: Ext.Msg.OK,
                            animEl: 'elId',
                            icon: Ext.MessageBox.INFO
                        });
                    }
                }
            },
            {
                tooltip: { text: _POI_DeleteSelected, autoHide: true },
                icon: 'images/delete.png',
                handler: function() {
                    var records = grdPOI.getSelectionModel().getSelections();
                    if (records.length > 0) {
                        Ext.MessageBox.YESNO = { yes: _TrackerMain_Yes, no: _TrackerMain_No };
                        Ext.MessageBox.confirm(_TrackerMain_Title, _POI_SureToDelete, function(btn) {
                            if (btn == "no") {
                            }
                            if (btn == "yes") {
                                var poiids = "";
                                for (i = 0; i < records.length; i++) {
                                    poiids += poiids == "" ? records[i].get('poiid') : "|" + records[i].get('poiid');
                                }
                                Ext.Ajax.request({
                                    url: 'HandlerDeletePOI',
                                    method: 'post',
                                    params: {
                                    poiids: poiids
                                    },
                                    success: function(btn) {
                                        var val = btn.responseText;
                                        if (val == 0) {
                                            Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
                                            Ext.Msg.show({
                                                title: _TrackerMain_Title,
                                                msg: _POI_DeleteSuccess,
                                                buttons: Ext.Msg.OK,
                                                animEl: 'elId',
                                                icon: Ext.MessageBox.INFO
                                            });
                                            //reload poi list
                                            poistore.reload({
                                                params: {
                                                    poikey: txtKey.getValue()
                                                }
                                            });
                                            if (imap != null) {
                                                imap.cancelAddEditPOI();
                                                if (imap.deleteSelectedPOI)
                                                    imap.deleteSelectedPOI(record.get("poiid"));
                                            }
                                        }
                                        else {
                                            Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
                                            Ext.Msg.show({
                                                title: _TrackerMain_Title,
                                                msg: _POI_DeleteFailed,
                                                buttons: Ext.Msg.OK,
                                                animEl: 'elId',
                                                icon: Ext.MessageBox.INFO
                                            });
                                        }
                                    }
                                });
                            }
                        }, this);


                    }
                    else {
                        Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
                        Ext.Msg.show({
                            title: _TrackerMain_Title,
                            msg: _POI_SelectAtLeastOne,
                            buttons: Ext.Msg.OK,
                            animEl: 'elId',
                            icon: Ext.MessageBox.INFO
                        });
                    }
                }
            }
            ,
            {
                xtype: 'tbseparator', width: 5
            },
            {
                tooltip: { text: _POI_ShowSelectedPOI, autoHide: true },
                icon: 'images/poi.gif',
                handler: function() {
                    if (grdPOI.getSelectionModel().selection != null) {
                        var records = grdPOI.getSelectionModel().selection.record;
                        if (imap != null)
                            imap.drawPOIbyRecords(records);
                    }
                    else {
                        Ext.MessageBox.buttonText.ok = _TrackerMain_OK;
                        Ext.Msg.show({
                            title: _TrackerMain_Title,
                            msg: _POI_SelectAtLeastOne,
                            buttons: Ext.Msg.OK,
                            animEl: 'elId',
                            icon: Ext.MessageBox.INFO
                        });
                    }
                }
            },
            {
                tooltip: { text: _POI_HidePOI, autoHide: true },
                icon: 'images/hidepoi.gif',
                handler: function() {
                    if (imap != null)
                        imap.hidePOI();
                }
            },
            {
                xtype: 'tbseparator', width: 5
            },
            chkShowAllPOI
        ]
    });

    function updatePoi(a,record) {
        Ext.MessageBox.show({
            msg: _CustomerConfigResource_Saving,
            progressText: _CustomerConfigResource_Saving,
            width: 300,
            wait: true,
            waitConfig: { interval: 200 }
        });
        grdPOI.stopEditing(true);
        submitEditPOI(record.get('poiid'), record.get('poiname'), record.get('latitude'), record.get('longitude'), record.get('iconindex'));

    }

    var smPOI = new Ext.grid.CheckboxSelectionModel();
    
    var grdPOI = new Ext.grid.EditorGridPanel({
        store: poistore,
        plugins: [editor],
        clicksToEdit: 2,
        cm:new Ext.grid.ColumnModel({
            columns: [
                    //smPOI, 
                    {header: _POI_Name, width: 200, sortable: true, menuDisabled: true, dataIndex: 'poiname', editor: new Ext.form.TextField({}) },
                    { header: _POI_Latitude, width: 75, sortable: true, menuDisabled: true, dataIndex: 'latitude', editor: new Ext.form.NumberField({
                        allowBlank: false,
                        blankText: _CustomerConfigResource_CanNotEmpty,
                        maxValue: 90,
                        maxText: _Message_MaxText + '90',
                        minText:_Message_MinText+'-90',
                        minValue: -90,
                        decimalPrecision:8
                        
                    })
                    },
                    { header: _POI_Longitude, width: 85, sortable: true, menuDisabled: true, dataIndex: 'longitude', editor: new Ext.form.NumberField({
                        allowBlank: false,
                        blankText: _CustomerConfigResource_CanNotEmpty,
                        maxValue: 180,
                        minValue: -180,
                        maxText: _Message_MaxText + '180',
                        minText: _Message_MinText + '-180',
                        decimalPrecision:8
                    })
                    }
                ]
                })
        , sm: smPOI
        ,width: 520
        ,height: 300
    });
    poistore.on('update', updatePoi);

//    grid.addListener('rowdblclick', function(obj, index) {
//        var record = grid.getStore().getAt(index);
//        var name = record.get("name");
//        var longitude = parseFloat(record.get("longitude"));
//        var latitude = parseFloat(record.get("latitude"));
//        var iconindex = parseInt(record.get("iconindex"));
//        var poiid = parseInt(record.get("poiid"));
//        
//        if (imap != null) {
//            imap.drawPOI(poiid, name, latitude, longitude, iconindex, true);
//        }

//    });
    var poiwindow = new Ext.Window({
        title: _POI_Title,
        layout:'fit',
        height: 370,
        width: 560,
        items: grdPOI,
        constrain:true,
        tbar: tbar,
        minimizable: true,
         listeners: {
            'minimize': function() {
                addTaskButtonForWindow(poiwindow);
            }
        }
    });

    poiwindow.show();
    poistore.reload({params: {poikey: ''}});
}